/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionbean;

import entitybean.Comments;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Thien Nguyen
 */
@Stateless
@org.jboss.ejb3.annotation.RemoteBinding(jndiBinding = "CommentsJNDI")
@org.jboss.ejb3.annotation.LocalBinding(jndiBinding = "CommentsLocalJNDI")
public class CommentsSessionBean implements CommentsSessionBeanRemote, CommentsSessionBeanLocal {

    @PersistenceContext(unitName = "J2EE_ShareMusic-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public List loadComment() {
        String jpql = "SELECT a FROM Comments a";
        javax.persistence.Query query = em.createQuery(jpql);
        List list = query.getResultList();
        return list;
    }

    public List getComment(int id) {
        String jpql = "SELECT a FROM Comments a WHERE a.commentID =:id";
        javax.persistence.Query query = em.createQuery(jpql);
        query.setParameter("id", id);
        List list = query.getResultList();
        return list;
    }

    public boolean deleteComment(int id) {
        Comments comment = em.find(Comments.class, id);
        if (comment != null) {
            em.remove(comment);
            return true;
        } else {
            return false;
        }
    }

    public boolean saveComment(int id, String content) {
        String jpql = "UPDATE Comments a SET a.content =:content WHERE a.commentID =:id";
        javax.persistence.Query query = em.createQuery(jpql);
        query.setParameter("content", content);
        query.setParameter("id", id);
        int result = query.executeUpdate();
        if(result > 0) return true;
        else return false;
    }
}
